package com.ncmmall.application.business.merchant.banner;

import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.domain.business.goods.QGoods;
import com.ncmmall.domain.business.merchant.banner.MerchantBanner;
import com.ncmmall.domain.business.merchant.banner.MerchantBannerRepository;
import com.ncmmall.domain.business.merchant.banner.QMerchantBanner;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

/**
 * @author linxs
 * @Description: 店铺海报
 * @Date: 2019-11-14 15:19
 */
@Service
@Transactional
public class BannerApplication {


    @Autowired
    private MerchantBannerRepository bannerRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    public void create(MerchantBanner banner) {

        banner.setStatus(1);
        banner.setCreateTime(new Timestamp(System.currentTimeMillis()));
        banner.setInnerLinkName(queryChannelService.findOne(QGoods.goods.id.eq(banner.getInnerLinkId()), Goods.class).getName());
        bannerRepository.save(banner);

    }

    public void modify(MerchantBanner params) {
        MerchantBanner banner = queryChannelService.findOne(QMerchantBanner.merchantBanner.id.eq(params.getId()), MerchantBanner.class);
        banner.setName(params.getName());
        banner.setImage(params.getImage());
        banner.setInnerLinkId(params.getInnerLinkId());
        banner.setInnerLinkName(queryChannelService.findOne(QGoods.goods.id.eq(params.getInnerLinkId()), Goods.class).getName());
        banner.setRank(params.getRank());
        bannerRepository.save(banner);
    }

    public void disable(long id) {
        MerchantBanner banner = queryChannelService.findOne(QMerchantBanner.merchantBanner.id.eq(id), MerchantBanner.class);
        banner.setStatus(1);
        bannerRepository.save(banner);
    }

    public void enabled(long id) {
        MerchantBanner banner = queryChannelService.findOne(QMerchantBanner.merchantBanner.id.eq(id), MerchantBanner.class);
        banner.setStatus(2);
        bannerRepository.save(banner);
    }

    public void remove(long id) {
        MerchantBanner banner = queryChannelService.findOne(QMerchantBanner.merchantBanner.id.eq(id), MerchantBanner.class);
        bannerRepository.delete(banner);
    }
}
